package tcc.model.graph;	

import java.util.ArrayList;
import java.util.List;

import tcc.view.graph.GraphDisplay;

/**
 * Represents a vertex in a graph 
 * @author Gabriel
 *
 */
public class Vertex implements Comparable<Vertex> {
	private Integer id;
	private Boolean goal = false;
	private int nEdges = 0; //number of edges
	private List<Edge> edges = new ArrayList<Edge>(); //list of edges
	private double positionX;
	private double positionY;
	private String rfid;
	/**
	 * Creates a vertex with ID and positions based on the Cartesian plane
	 * @param id ID of the vertex
	 * @param posX Position X of the vertex on the Cartesian plane
	 * @param posY Position Y of the vertex on the Cartesian plane
	 */
	public Vertex (int id, double posX, double posY, String rfid) {
		if(posX <= GraphDisplay.width - 100 &&
			posY <= GraphDisplay.height - 100
			) {
				
				this.id = id;
				this.positionX = posX;
				this.positionY = posY;
				this.rfid = rfid;
		}
	}
	
	public Vertex getVertex(){
		return this;
	}
	
	/**
	 * set a vertex as a goal and define the pheromone tax 
	 * of all edges linked to this vertex to 100 
	 */
	public void setAsGoal() { //FIXME
		this.goal = true;
		for(Edge e : this.edges) {
			e.setPheromone(100);
		}
	}
	
	/**
	 * set a vertex as not a goal and define the pheromone tax 
	 * of all edges linked to this vertex to 1
	 */
	public void setAsNotGoal(){
		this.goal = false;
		for(Edge e : this.edges){
			e.setPheromone(1);
		}
	}
	
	public Boolean getGoal() {
		return this.goal;
	}
	
	public int getId() {
		return this.id;
	}
	
	public String getRfid() {
		return this.rfid;
	}

	public int getNEdges() {
		return nEdges;
	}

	public void setNEdges(int increment) {
		this.nEdges += increment;
	}
	
	
	public List<Edge> getEdges() {
		return edges;
	}

	public void addEdges(Edge edge) {
		if(!this.edges.contains(edge)) {
			this.edges.add(edge);
		}
		
	}

	public double getPositionX() {
		return this.positionX;
	}
	
	public double getPositionY() {
		return this.positionY;
	}
	
	public Boolean isGoal() {
		return this.goal;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Vertex other = (Vertex) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	public int compareTo(Vertex v) {
		if(this.getId() > v.getId()) {
			return 1;
		}else {
			return -1;
		}
	}
}